//
//  UIImage+SCBaseExt.m
//  SCFoundationKit
//
//  Created by Chao Sheng on 2023/10/17.
//

#import "UIImage+SCBaseExt.h"

@implementation UIImage (SCBaseExt)

+ (UIImage *)sc_imageGradientStyleColors:(NSArray *)colors
                           Position:(SCGradientPosition)position
                               Size:(CGSize)size {
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    NSMutableArray *cgColors = @[].mutableCopy;
    for(UIColor *color in colors) {
        [cgColors addObject:(id)color.CGColor];
    }
    gradientLayer.colors = cgColors;
    gradientLayer.locations = @[@0.0,@1.0];
    if (position == SCGradientPositionLeftToRight) {
        gradientLayer.startPoint = CGPointMake(0, 0.5);
        gradientLayer.endPoint = CGPointMake(1, 0.5);
    } else if (position == SCGradientPositionRightToLeft) {
        gradientLayer.startPoint = CGPointMake(1, 0.5);
        gradientLayer.endPoint = CGPointMake(0, 0.5);
    } else if (position == SCGradientPositionBottomToTop) {
        gradientLayer.startPoint = CGPointMake(0.5, 1);
        gradientLayer.endPoint = CGPointMake(0.5, 0);
    } else {
        gradientLayer.startPoint = CGPointMake(0.5, 0);
        gradientLayer.endPoint = CGPointMake(0.5, 1);
    }
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContext(size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
